<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name=viewport content="width=device-width">
    <title>JSSDKMainPage</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>
<body>
<div>
    <div class="container">
        <h1>Weixin Express for JSSDK</h1>

        <h2><a class="btn btn-success QRScan">测试扫码</a></h2>

    </div>

    <script>
        $(document).ready(function () {
            var url = "<%=jssdk.config%>";
            alert(location.origin + location.pathname);
            $.ajax({
                url: url, //这个地址是服务器配置JSSDK的地址
                data: {
                    url: location.origin + location.pathname
                    //这个地址是发生jssdk调用的url地址
                }
            }).success(function (json) {
                alert(JSON.stringify(json.data));
                var data = json.data;
                var config = {};
                for (var k in json.data) {
                    config[k] = json.data[k];
                }

                config.jsApiList = [
                    'checkJsApi',
                    'scanQRCode'
                ];
                config.timestamp = parseInt(config.timestamp);
                console.log(config);
                wx.config(config);
                wx.ready(function () {
                    alert("微信jssdk配置成功!");
                    $('h2 > a.QRScan').click(function () {
                        wx.scanQRCode({
                            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                            success: function (res) {
                                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                                alert(result);
                            }
                        });
                    });
                });
                wx.error(function (res) {
                    alert('微信JSSDK配置失败！');
                    alert(JSON.stringify(res));
                    alert(res.errMsg);
                });
            });
        });

    </script>
</div>
</body>
</html>
